package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemand;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.EnterpriseSupplyDemandParam;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemandMapper;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppEnterpriseSupplyDemandService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemandCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemandSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemandUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemandDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemandListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 企业供需实现类
 * @author LikeAdmin
 */
@Service
public class TAppEnterpriseSupplyDemandServiceImpl implements ITAppEnterpriseSupplyDemandService {
        
    @Resource
    TAppEnterpriseSupplyDemandMapper tAppEnterpriseSupplyDemandMapper;

    /**
     * 企业供需列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppEnterpriseSupplyDemandListedVo>
     */
    @Override
    public PageResult<TAppEnterpriseSupplyDemandListedVo> list(PageValidate pageValidate, TAppEnterpriseSupplyDemandSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppEnterpriseSupplyDemand> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppEnterpriseSupplyDemandMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:type:int",
            "like:projectName@project_name:str",
            "=:expectedProjectDeliveryDate@expected_project_delivery_date:str",
            "=:industryId@industry_id:int",
            "=:provinceId@province_id:int",
            "=:cityId@city_id:int",
            "=:cooperationAmount@Cooperation_amount:long",
            "=:projectIntroduce@project_introduce:str",
            "=:phone:str",
            "=:enterpriseId@enterprise_id:int",
            "=:status:int",
            "=:userId@user_id:int",
        });

        IPage<TAppEnterpriseSupplyDemand> iPage = tAppEnterpriseSupplyDemandMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppEnterpriseSupplyDemandListedVo> list = new LinkedList<>();
        for(TAppEnterpriseSupplyDemand item : iPage.getRecords()) {
            TAppEnterpriseSupplyDemandListedVo vo = new TAppEnterpriseSupplyDemandListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 企业供需详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppEnterpriseSupplyDemand
     */
    @Override
    public TAppEnterpriseSupplyDemandDetailVo detail(Integer id) {
        TAppEnterpriseSupplyDemand model = tAppEnterpriseSupplyDemandMapper.selectOne(
                new QueryWrapper<TAppEnterpriseSupplyDemand>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppEnterpriseSupplyDemandDetailVo vo = new TAppEnterpriseSupplyDemandDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 企业供需新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppEnterpriseSupplyDemandCreateValidate createValidate) {
        TAppEnterpriseSupplyDemand model = new TAppEnterpriseSupplyDemand();
        model.setType(createValidate.getType());
        model.setProjectName(createValidate.getProjectName());
        model.setExpectedProjectDeliveryDate(createValidate.getExpectedProjectDeliveryDate());
        model.setIndustryId(createValidate.getIndustryId());
        model.setProvinceId(createValidate.getProvinceId());
        model.setCityId(createValidate.getCityId());
        model.setCooperationAmount(createValidate.getCooperationAmount());
        model.setProjectIntroduce(createValidate.getProjectIntroduce());
        model.setPhone(createValidate.getPhone());
        model.setEnterpriseId(createValidate.getEnterpriseId());
        model.setStatus(createValidate.getStatus());
        model.setUserId(createValidate.getUserId());
        tAppEnterpriseSupplyDemandMapper.insert(model);
    }

    /**
     * 企业供需编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppEnterpriseSupplyDemandUpdateValidate updateValidate) {
        TAppEnterpriseSupplyDemand model = tAppEnterpriseSupplyDemandMapper.selectOne(
                new QueryWrapper<TAppEnterpriseSupplyDemand>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setType(updateValidate.getType());
        model.setProjectName(updateValidate.getProjectName());
        model.setExpectedProjectDeliveryDate(updateValidate.getExpectedProjectDeliveryDate());
        model.setIndustryId(updateValidate.getIndustryId());
        model.setProvinceId(updateValidate.getProvinceId());
        model.setCityId(updateValidate.getCityId());
        model.setCooperationAmount(updateValidate.getCooperationAmount());
        model.setProjectIntroduce(updateValidate.getProjectIntroduce());
        model.setPhone(updateValidate.getPhone());
        model.setEnterpriseId(updateValidate.getEnterpriseId());
        model.setStatus(updateValidate.getStatus());
        model.setUserId(updateValidate.getUserId());
        tAppEnterpriseSupplyDemandMapper.updateById(model);
    }

    /**
     * 企业供需删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppEnterpriseSupplyDemand model = tAppEnterpriseSupplyDemandMapper.selectOne(
                new QueryWrapper<TAppEnterpriseSupplyDemand>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppEnterpriseSupplyDemandMapper.delete(new QueryWrapper<TAppEnterpriseSupplyDemand>().eq("id", id));
    }

    /**
     * 查询用户指定企业下的我得供应/我得需求
     * @param param
     * @return
     */
    @Override
    public String selectEnterpriseSupplyDemandList(EnterpriseSupplyDemandParam param) {
        Integer userId = param.getUserId();
        Integer enterpriseId = param.getEnterpriseId();
        Integer type = param.getType();
        if(userId==null || enterpriseId==null || type==null){
            return JsonReturn.fail("必传参数不能为空",201).toJsonStr();
        }
        List<TAppEnterpriseSupplyDemand> selectList = tAppEnterpriseSupplyDemandMapper.selectList(
                new QueryWrapper<TAppEnterpriseSupplyDemand>().eq("user_id", userId)
                        .eq("enterprise_id", enterpriseId).eq("type", type));
        return JsonReturn.success().setListData(Optional.ofNullable(selectList).orElse(new ArrayList<>())).toJsonStr();
    }

    /**
     * 根据企业供需id查询企业供需（用于分享链接跳转页面之后进行加载数据）
     * @param enterpriseSupplyDemandId
     * @return
     */
    @Override
    public String selectEnterpriseSupplyDemandById(Integer enterpriseSupplyDemandId) {
        if(enterpriseSupplyDemandId==null){
            return JsonReturn.fail("必传参数不能为空",201).toJsonStr();
        }
        TAppEnterpriseSupplyDemand appEnterpriseSupplyDemand = tAppEnterpriseSupplyDemandMapper.selectById(enterpriseSupplyDemandId);
        return JsonReturn.success().put("appEnterpriseSupplyDemand",Optional.ofNullable(appEnterpriseSupplyDemand).orElse(new TAppEnterpriseSupplyDemand())).toJsonStr();
    }

    /**
     * 企业发布新的供需
     * @param appEnterpriseSupplyDemand
     * @return
     */
    @Override
    public String insertAppEnterpriseSupplyDemand(TAppEnterpriseSupplyDemand appEnterpriseSupplyDemand) {
        Integer userId = appEnterpriseSupplyDemand.getUserId();
        Integer enterpriseId = appEnterpriseSupplyDemand.getEnterpriseId();
        Integer type = appEnterpriseSupplyDemand.getType();
        if(userId==null || enterpriseId==null || type==null){
            return JsonReturn.fail("必传参数不能为空",201).toJsonStr();
        }
        appEnterpriseSupplyDemand.setStatus(STATUS.STATUS_PENDING_APPROVAL);
        tAppEnterpriseSupplyDemandMapper.insert(appEnterpriseSupplyDemand);
        return JsonReturn.success().toJsonStr();
    }




    public static class STATUS{
        //待审核
        public static final Integer STATUS_PENDING_APPROVAL = 1;
        //已通过
        public static final Integer STATUS_PASSED = 2;
        //未通过
        public static final Integer STATUS_FAIL = 3;
    }

}
